import * as Api from '../../utils/api.js'
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    color: [],
    datilModel: false,
    showModelRela: false,
    productTotle: 0,
    basicMall: null,
    model: false,
    goodsId: '',
    info: [],
    num: 1,
    imgUrls: [],
    hideCart: false,
    carInfo: [],
    themeColors: '',
    seniorMall:false,
    windowWidth: 375,
    basisList: [
      {
        title: '商品品牌',
        valueKey: 'goodsBrand'
      },
      // {
      //   title: 'OEM号',
      //   valueKey: 'oemCode'
      // },
      {
        title: '商品编码',
        valueKey: 'goodsModelNumber'
      },
      {
        title:'OEM码',
        valueKey: 'oemCode'
      },
      {
        title: '横向件专属码',
        valueKey: 'exclusiveCode'
      },
      // {
      //   title: '配件品质',
      //   valueKey: 'qualityRange'
      // }
    ],
    contentUrl: [

    ]
  },
  changeDatilModel() {

    this.setData({
      datilModel: !this.data.datilModel
    })
  },
  goPdbindCarList() {
    console.log(this.data.goodsId)
    wx.navigateTo({
      url: '../product_car/product_car?goodsId=' + this.data.goodsId,
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })

  },
  bindBody() {
    return
  },
  lookImageProduct(e) {
    console.log(e.currentTarget.dataset)
    wx.previewImage({
      current: e.currentTarget.dataset.url, // 当前显示图片的http链接
      urls: e.currentTarget.dataset.main // 需要预览的图片http链接列表
    })

  },
  //S端查看图片
  lookImageSduan: function (e) {
    if (e.currentTarget.dataset.main.contentType === 'IMAGE') {
      wx.previewImage({
        current: '', // 当前显示图片的http链接
        urls: e.currentTarget.dataset.main.url.split(',') // 需要预览的图片http链接列表
      })
    } else {
      return
    }
  },
  lookProductImage: function (e) {
    console.log(e.currentTarget.dataset.main)
    wx.previewImage({
      current: '', // 当前显示图片的http链接
      urls: e.currentTarget.dataset.main // 需要预览的图片http链接列表
    })
  },

  //B端查看图片
  lookImage: function (e) {
    if (e.currentTarget.dataset.main.contentType === 'IMAGE') {
      console.log(e.currentTarget.dataset.main)
      wx.previewImage({
        current: '', // 当前显示图片的http链接
        urls: e.currentTarget.dataset.main.contentUrl.split(',') // 需要预览的图片http链接列表
      })

      // wx.navigateTo({
      //   url: '../show_image/show_image?contentUrl=' + e.currentTarget.dataset.main.contentUrl,
      //   success: function (res) { },
      //   fail: function (res) { },
      //   complete: function (res) { },
      // })
    } else {
      return
    }
  },
  addnum: function (e) {
    if (e.target.dataset.type == 'add') {

      this.setData({
        num: this.data.num + 1
      })
    } else {
      console.log(111)
      if (this.data.num === 1) {
        return
      }
      this.setData({
        num: this.data.num - 1
      })
    }
    let pro = this.data.info.memberPrice * this.data.num
    pro = pro.toFixed(2)
    this.setData({
      productTotle: pro
    })
  },
  changeModel: function (e) {

    if (e.currentTarget.dataset.type) {

      this.setData({
        shopModel: e.currentTarget.dataset.type
      })
    }
    this.setData({
      model: !this.data.model

    })
  },
  // onShareAppMessage: function (res) {
  //   console.log('转发',res)
  // let self = this;
  //   return {
  //     path: '/pages/example/product_datil/product_datil?id=' + self.data.goodsId,
  //     success: function (res) {
  //       console.log(res)
  //       // 转发成功
  //     },
  //     fail: function (res) {
  //       console.log(res)
  //       // 转发失败
  //     }
  //   }
  // },
  /**
  * 生命周期函数--监听页面加载
  */
  onLoad: function (options) {
    if (app.globalData.merchant === null) {
      app.doLogin(this.onLoad, this.onShow, options);
      return
    }
    console.log(777)
    console.log(options)
    if (options.oem){
      this.setData({
        hideCart: true
      })
    }
    this.setData({
      basicMall: app.globalData.merchant.basicMall,
      showModelRela: app.globalData.merchant.showModelRela,
      goodsId: options.id,
      carId: options.carId,
      themeColors: app.globalData.merchant.themeColors,
      seniorMall: app.globalData.merchant.seniorMall
    })

    this.setData({
      windowWidth: app.globalData.windowWidth
    })




  },

  getReCatDatil() {

    let self = this
    let url = app.globalData.baseUrl1 + '/goods/front/simpledetail'
    let data = {
      token: app.globalData.token,
      userId: '',
      goodsId: this.data.goodsId,//商品id
    }
    if (app.globalData.userId !== null) {
      data.userId = app.globalData.userId
    }
    //发起网络请求
    let callBack = (res) => {

      if (res == null) {
        console.log("网络请求错误！");
        return;
      }

      if (res.statusCode != 200) {
        console.log("请求数据错误");

        return;
      }
      if (res.statusCode === 200) {
        console.log('商品详情', res)
        if (res.data.status === 404) {
          wx.showModal({
            title: '',
            content: res.data.message,
            showCancel: false,
            cancelText: '',
            cancelColor: '',
            confirmText: '知道了',
            confirmColor: '',
            success: function (res) {
              wx.navigateBack({
                delta: 1,
              })
            },
            fail: function (res) { },
            complete: function (res) { },
          })
          return
        }
        self.setData({
          num: 1,
          info: res.data.data,
          imgUrls: res.data.data.imageList.split(','),
          productTotle: res.data.data.memberPrice,
          color: res.data.data.color ? res.data.data.color.split(',') : []
        })
      }
    }
    Api.reqFormPost(url, data, callBack)
  },
  getDatil() {
    let self = this
    let url = app.globalData.baseUrl1 + '/goods/front/detail'
    let data = {}

    if (app.globalData.userId == null) {
      data = {
        merchantId: app.globalData.merchant.merchantId,
        goodsId: this.data.goodsId,//商品id
        carId: this.data.carId
      }
    } else {
      data = {
        merchantId: app.globalData.merchant.merchantId,
        userId: app.globalData.userId,
        goodsId: this.data.goodsId,//商品id
        carId: this.data.carId
      }
    }


    //发起网络请求
    let callBack = (res) => {

      if (res == null) {
        console.log("网络请求错误！");
        return;
      }

      if (res.statusCode != 200) {
        console.log("请求数据错误");
        return;
      }
      if (res.statusCode === 200) {
        console.log('商品详情', res.data.data)
        self.setData({
          num: 1,
          info: res.data.data,
          imgUrls: res.data.data.imageList.split(','),
          productTotle: res.data.data.memberPrice,
          color: res.data.data.color ? res.data.data.color.split(',') : []
        })
      }
    }
    Api.reqFormPost(url, data, callBack)
  },


  addCar: function (e) {

    if (app.globalData.userId == null) {
      wx.navigateTo({
        url: '/pages/core/login/login',
      })
      return
    }

    let self = this
    console.log(e)

    if (this.data.shopModel === 'cart') {
      let url = app.globalData.baseUrl1 + '/shopcart/add'
      let data = {
        token: app.globalData.token,
        userId: app.globalData.userId,
        goodsId: this.data.goodsId,
        skuId: this.data.info.sku[0].skuId,
        num: this.data.num
      }
      let callBack = (res) => {

        if (res == null) {

          console.log("网络请求错误！");
          return;
        }

        if (res.data.status != 200) {
          console.log("请求数据错误");
          return;
        }

        if (res.data.status == 200) {

          wx.showToast({
            title: '成功',
            icon: 'success',
            duration: 2000, success() {
              self.setData({
                num: 1,
                productTotle: self.data.info.memberPrice
              })
            }
          })

          self.changeModel(e)

        }
      }
      Api.reqFormPost(url, data, callBack)
    } else {
      console.log('购买，确定')
      self.changeModel(e)
      wx.navigateTo({
        url: '../order_list/order_list?pagetype=shop&goodsid=' + this.data.goodsId + '&num=' + this.data.num + '&skuid=' + this.data.info.sku[0].skuId + '&carid=' + this.data.carId,
        complete(e) {
          console.log(e)
        }
      })
    }

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    if (app.globalData.merchant === null) {
      return
    }
    this.getCarInfo()
    if (app.globalData.userId === null) {
      if (app.globalData.merchant.mustLogin) {
        wx.navigateTo({
          url: '/pages/core/login/login',
          success() {
            wx.showToast({
              title: '请先登陆',
              icon: 'loading',
              duration: 1000
            })
          }
        })
        return
      }
    }


    this.setData({
      model: false
    })


    if (this.data.carId) {
      this.getDatil()
    } else {
      this.getReCatDatil()
    }

  },
  getCarInfo() {
    let self = this
    // 是否启用力洋接口
    let url = app.globalData.baseUrl + '/bsm'
    let data = {
      modelId: app.globalData.carId ? app.globalData.carId :'',
      merchantId: app.globalData.merchant.merchantId
    }
    //发起网络请求
    let callBack = (res) => {

      if (res == null) {
        console.log("网络请求错误！");
        return;
      }

      if (res.statusCode != 200) {
        console.log("请求数据错误");
        return;
      }
      console.log(res.data.data)
      if (res.statusCode === 200) {
        self.setData({
          carInfo: res.data.data
        })

      }
    }
    Api.reqFormPost(url, data, callBack)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})